unit uUserRegistration;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uDM, DynamicSkinForm, ComCtrls, SkinTabs, StdCtrls, SkinCtrls, Mask,
  SkinBoxCtrls, spdbctrls, dxtree, dxdbtree, RzTreeVw, Grids, BaseGrid, AdvGrid,
  AdvObj;

type
  TFrmUserRegistration = class(TForm)
    spDynamicSkinForm1: TspDynamicSkinForm;
    spSkinPageControl1: TspSkinPageControl;
    spSkinTabSheet1: TspSkinTabSheet;
    spSkinTabSheet2: TspSkinTabSheet;
    spSkinStdLabel1: TspSkinStdLabel;
    spSkinStdLabel2: TspSkinStdLabel;
    UserName: TspSkinEdit;
    spSkinStdLabel3: TspSkinStdLabel;
    spSkinStdLabel4: TspSkinStdLabel;
    Jabatan: TspSkinEdit;
    BtnJabatan: TspSkinButton;
    Pass1: TspSkinDBPasswordEdit;
    Pass2: TspSkinDBPasswordEdit;
    MenuList: TRzTreeView;
    Tambah: TspSkinCheckRadioBox;
    Edit: TspSkinCheckRadioBox;
    Hapus: TspSkinCheckRadioBox;
    Cetak: TspSkinCheckRadioBox;
    BtnNext: TspSkinButton;
    BtnBack: TspSkinButton;
    BtnSave: TspSkinButton;
    BtnNew: TspSkinButton;
    BtnEdit: TspSkinButton;
    BtnDelete: TspSkinButton;
    grid: TAdvStringGrid;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure BtnJabatanClick(Sender: TObject);
    procedure JabatanKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormShow(Sender: TObject);
    procedure BtnSaveClick(Sender: TObject);
    procedure BtnNextClick(Sender: TObject);
    procedure BtnBackClick(Sender: TObject);
    procedure UserNameKeyPress(Sender: TObject; var Key: Char);
    procedure MenuListClick(Sender: TObject);
    procedure gridCanEditCell(Sender: TObject; ARow, ACol: Integer;
      var CanEdit: Boolean);
    procedure BtnEditClick(Sender: TObject);
    procedure gridDblClick(Sender: TObject);
    procedure BtnNewClick(Sender: TObject);
    procedure BtnDeleteClick(Sender: TObject);
  private
    { Private declarations }
    Alist: TStringList;
    function isValid: Boolean;
    function isSaved: Boolean;
    Procedure LoadData(Auser: String);
    Procedure LoadUser;
    Procedure InitGrid;
    Procedure InitForm;
    Procedure SetColWidth;
  public
    { Public declarations }
  end;

var
  FrmUserRegistration: TFrmUserRegistration;
const
  CNo = 0;
  CUserName = 1;
  CJabatan = 2;

implementation

uses uLookup;

{$R *.dfm}

procedure TFrmUserRegistration.BtnDeleteClick(Sender: TObject);
begin
if UserName.Tag = 0 then Alert(MSG_NO_DATA)
else begin
  if Confirm(MSG_CONFIRM_DELETE) then begin
    ExecSQL(DM.Buffer, 'delete from user_form where IDUser='+QuotedStr(UserName.Text));
    ExecSQL(DM.Buffer, 'delete from user_dat where username='+QuotedStr(UserName.Text));
    LoadData('');
  end;
end;
end;

procedure TFrmUserRegistration.BtnEditClick(Sender: TObject);
begin
  LoadData(grid.Cells[CUserName, grid.Row]);
end;

procedure TFrmUserRegistration.BtnJabatanClick(Sender: TObject);
begin
  OpenSQL(DM.Lookup, 'Select ID, Jabatan, Level From jabatan order by Level, Jabatan');
  LoadFormLookup;
  if FrmLookup.ModalResult = mrOk then
  begin
    Jabatan.Tag := DM.Lookup.Fields[0].AsInteger;
    Jabatan.Text := DM.Lookup.Fields[1].AsString;
  end;
end;

procedure TFrmUserRegistration.BtnNewClick(Sender: TObject);
begin
  InitForm;
end;

procedure TFrmUserRegistration.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  FreeAndNil(Alist);
  Action := caFree;
end;

procedure TFrmUserRegistration.FormShow(Sender: TObject);
begin
  spSkinPageControl1.ActivePageIndex := 0;
  Alist := TStringList.Create;
  InitForm;
  InitGrid;
  LoadData('');
end;

procedure TFrmUserRegistration.gridCanEditCell(Sender: TObject; ARow,
  ACol: Integer; var CanEdit: Boolean);
begin
  CanEdit := false;
end;

procedure TFrmUserRegistration.gridDblClick(Sender: TObject);
begin
  BtnEditClick(sender);
end;

procedure TFrmUserRegistration.InitForm;
begin
  UserName.Clear;
  UserName.Tag := 0;
  Pass1.Clear;
  Pass2.Clear;
  Jabatan.Clear;
  Jabatan.Tag := 0;
  Alist.Clear;
  UserName.ReadOnly := false;
  UserName.SetFocus;
end;

procedure TFrmUserRegistration.InitGrid;
begin
with grid do begin
  RowCount := 2;
  ColCount := CJabatan + 2;
  Clear;
  Cells[CNo, 0] := 'No.';
  Cells[CUserName, 0] := 'Username';
  Cells[CJabatan, 0] := 'Jabatan';
  AutoSize := True;
end;
end;

function TFrmUserRegistration.isSaved: Boolean;
var i : integer;
begin
try
  DM.Connection.StartTransaction;
  ExecSQL(DM.Buffer, 'delete from user_form where IDUser='+QuotedStr(UserName.Text));
  ExecSQL(DM.Buffer, 'delete from user_dat where username='+QuotedStr(UserName.Text));
  ExecSQL(DM.Buffer, 'insert into user_dat (username, password, level) '+
  'values ('+QuotedStr(UserName.Text)+','+QuotedStr(Pass2.Text)+','+IntToStr(Jabatan.Tag)+')');
  if UserName.Tag = 0 then Alist.Clear;
   for i := 0 to MenuList.Items.Count - 1 do begin
     if MenuList.Items[i].Selected then
     begin
       Alist.Add(IntToStr(MenuList.Items[i].StateIndex));
     end;
  end;

  for i := 0 to Alist.Count - 1 do begin
      ExecSQL(DM.Buffer,'insert into user_form (IDUser, FormTag, isSave, isEdit, isDelete, isPrint) '+
      'Values ('+QuotedStr(UserName.Text)+','+Alist.Strings[i]+','+
      BoolToStr(Tambah.Checked)+','+BoolToStr(Edit.Checked)+','+
      BoolToStr(Hapus.Checked)+','+BoolToStr(Cetak.Checked)+')');
  end;

//  for i := 0 to MenuList.Items.Count - 1 do begin
//     if MenuList.Items[i].Selected then
//     begin
//        ExecSQL(DM.Buffer,'insert into user_form (IDUser, FormTag, isSave, isEdit, isDelete, isPrint) '+
//        'Values ('+QuotedStr(UserName.Text)+','+IntToStr(MenuList.Items[i].StateIndex)+','+
//        BoolToStr(Tambah.Checked)+','+BoolToStr(Edit.Checked)+','+
//        BoolToStr(Hapus.Checked)+','+BoolToStr(Cetak.Checked)+')');
//     end;
//  end;
  DM.Connection.Commit;
  Inform(MSG_SAVE_SUCCES);
  Close;
except
  DM.Connection.Rollback;
  Alert(MSG_SAVE_UNSUCCES);
end;

end;

function TFrmUserRegistration.isValid: Boolean;
begin
Result:= False;
  if CekInput(Result, UserName.Text <> '', 'Username belum diisi.', UserName) then
  if CekInput(Result, Pass1.Text <> '', 'Password belum diisi.', Pass1) then
  if CekInput(Result, Pass1.Text = Pass2.Text, 'Password belum sesuai.', Pass1) then
  if CekInput(Result, Jabatan.Text <> '', 'Jabatan belum sesuai.', Jabatan) then
     CekInput(Result, Alist.Count > 0, 'Accss Menegement belum sesuai.', BtnNext);
  if Alist.Count = 0 then spSkinPageControl1.ActivePageIndex := 1;
end;

procedure TFrmUserRegistration.JabatanKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key=vk_f2 then BtnJabatanClick(Sender);
end;

procedure TFrmUserRegistration.LoadData(Auser: String);
var i : integer;
begin
if Auser='' then begin
  OpenSQL(DM.Buffer, 'SELECT distinct U.username, J.Jabatan '+
  'FROM user_dat U LEFT JOIN jabatan J ON U.level=J.Id '+
  'LEFT JOIN user_form F on F.IDUser=U.username');
  if DM.Buffer.RecordCount > 0 then begin
  InitGrid;
    for i := 1 to DM.Buffer.RecordCount do begin
      grid.RowCount := 1 + i;
      grid.Ints[CNo, i] := i;
      grid.Cells[CUserName, i] := DM.Buffer.Fields[0].AsString;
      grid.Cells[CJabatan, i] := DM.Buffer.Fields[1].AsString;
      DM.Buffer.Next;
    end;
    grid.AutoSize := True;
  end;
end
else begin
  OpenSQL(DM.Buffer, 'SELECT * FROM user_dat U LEFT JOIN jabatan J ON U.level=J.Id '+
  'LEFT JOIN user_form F on F.IDUser=U.username WHERE U.username='+QuotedStr(Auser));
  if DM.Buffer.RecordCount > 0 then begin
     UserName.Tag := 1;
     UserName.Text := DM.Buffer.Fields[0].AsString;
     Pass1.Text := DM.Buffer.Fields[1].AsString;
     Pass2.Text := DM.Buffer.Fields[1].AsString;
     Jabatan.Tag:= DM.Buffer.Fields[3].AsInteger;
     Jabatan.Text := DM.Buffer.Fields[4].AsString;
     UserName.ReadOnly := True;
     for i := 0 to DM.Buffer.RecordCount do begin
        Alist.Add(DM.Buffer.Fields[8].AsString);
        DM.Buffer.Next;
     end;
   end;
end;
end;


procedure TFrmUserRegistration.LoadUser;
begin

end;

procedure TFrmUserRegistration.MenuListClick(Sender: TObject);
var i : integer;
begin
  UserName.Tag := 0;
  Alist.Clear;
   for i := 0 to MenuList.Items.Count - 1 do begin
     if MenuList.Items[i].Selected then
     begin
       Alist.Add(IntToStr(MenuList.Items[i].StateIndex));
     end;
  end;
end;

procedure TFrmUserRegistration.SetColWidth;
begin
  grid.AutoSize := true;
end;

procedure TFrmUserRegistration.BtnNextClick(Sender: TObject);
begin
  spSkinPageControl1.SelectNextPage(True, False);
end;

procedure TFrmUserRegistration.BtnBackClick(Sender: TObject);
begin
  spSkinPageControl1.SelectNextPage(False, True);
end;

procedure TFrmUserRegistration.BtnSaveClick(Sender: TObject);
begin
if isValid then
  isSaved;
end;

procedure TFrmUserRegistration.UserNameKeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13 then begin
    LoadData(UserName.Text);
    SelectNext(ActiveControl, true, true);
  end;
end;

end.

